//const apidomain ="dgjdev.test.com";
//const baseurl = 'https//'+apidomain+'/addons/jyshop';
const apidomain ="jyshop.qiwen110.com";
const baseurl = 'https://'+apidomain+'/addons/jyshop';
  const getToken = () => {
 	let token = uni.getStorageSync("token");
 	console.log("当前token=" + token);
 	return token;
 	 // return "abcde";//
 }

 const hasLogin = getToken() ? true : false;

 const setToken = (token) => {
 	console.log("设置token" + token);
 	uni.setStorageSync("token", token);
 }

 const logout = () => {
 	uni.removeStorage({
 		key: "token"
 	});
 }
 const parseQueryString = (url) => {
 	let obj = {};
 	let keyvalue = [];
 	let key = "",
 		value = "";
 	let paraString = url.substring(url.indexOf("?") + 1, url.length).split("&");
 	for (let i in paraString) {
 		keyvalue = paraString[i].split("=");
 		key = keyvalue[0];
 		value = keyvalue[1];
 		obj[key] = value;
 	}
 	return obj;
 }
 const req = (url, params, needlogin = false, success = null, fail = null) => {
 	let token = getToken();
 	if (needlogin && !token) {
 		toast("请先登录");
 		return false;
 	}
 	let header = {};
 	if (token) {
 		header.token = token;
 		// params = Object.assign({},{token:token},params);
 	}


 	console.log("req url=" + baseurl + url);
 	console.log(params);
 	let reqobj = {
 		"method": "POST",
 		"url": baseurl + url,
 		header: header,
 		"data": params,
 	};
 	if (success) {
 		reqobj.success = (res) => {

 			console.log(res);

 			let result = res.data;
 			if (result.code != 1) { //有错误
 				uni.showToast({
 					title: result.msg
 				})
 				fail && fail(result);
 				return;
 			}
 			success && success(result);
 		};

 	}
 	return uni.request(reqobj);
 }
 const navigate = (url, needlogin = false) => {
 	if (needlogin && !getToken()) {  
 		 
 		uni.showToast({
 			title: "请先登录"

 		})
 		setTimeout(function() {
 			uni.navigateTo({
 				url: "/pages/public/login"
 			});
 		}, 1000);
 		return;
 	}
 	console.log(url);
 	uni.navigateTo({
 		url: url
 	});
 }

 const toast = (msg, cbk = null, timeout = 300) => {
 	uni.showToast({
 		title: msg

 	});
 	console.log(cbk);
 	cbk && setTimeout(cbk, timeout);

 }
 var timestamp2date = function(var_timestamp, format) {
 	var newDate = new Date();
 	newDate.setTime(var_timestamp * 1000);
 	Date.prototype.format = function(format) {
 		var date = {
 			"M+": this.getMonth() + 1,
 			"d+": this.getDate(),
 			"h+": this.getHours(),
 			"m+": this.getMinutes(),
 			"s+": this.getSeconds(),
 			"q+": Math.floor((this.getMonth() + 3) / 3),
 			"S+": this.getMilliseconds()
 		};
 		if (/(y+)/i.test(format)) {
 			format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
 		}
 		for (var k in date) {
 			if (new RegExp("(" + k + ")").test(format)) {
 				format = format.replace(RegExp.$1, RegExp.$1.length == 1 ?
 					date[k] : ("00" + date[k]).substr(("" + date[k]).length));
 			}
 		}
 		return format;
 	}
 	// console.log(newDate.format('yyyy-MM-dd h:m:s'));
 	if (format) {
 		return newDate.format(format);
 	}
 	return newDate.format('yyyy-MM-dd hh:mm:ss');
 }
    const weektool=(type)=>{
		type = ~~type;
	  let	nowTime = new Date();
		let dayInWeek = nowTime.getDay();
		 dayInWeek == 0 && (dayInWeek = 7);
		let thsiWeekFirstDay = nowTime.getTime() - (dayInWeek - 1) * 86400000;
		let thisWeekLastDay = nowTime.getTime() + (7 - dayInWeek) * 86400000;
		let firstDay = thsiWeekFirstDay + type * 7 * 86400000;
		let lastDay = thisWeekLastDay + type * 7 * 86400000;	
		return [formateDate(firstDay),formateDate(lastDay)];
	}
    const formateDate=(time) =>{
     var newTime = new Date(time)
     var year = newTime.getFullYear();
     var month = newTime.getMonth() + 1;
     var day = newTime.getDate();
     return year + "-" + (month >= 10 ? month : "0" + month) + "-" + (day >= 10 ? day : "0" + day);
    };
 

 export default {
 	req,
 	toast,
 	navigate,
 	timestamp2date,
 	 apidomain,
 	baseurl,
 	getToken,
 	setToken,
 	hasLogin,
 	logout,
	parseQueryString,
	weektool,
 }
